<template>
	<view class="container ">
		<u-form class='form flex u-flex-col j-s'>
			<view class="padh20 padv150 font38" style="padding-top: 20%;">
				<view>欢迎来到</view>
				<view class="padv20">慎独家政</view>
			</view>
			<view class="flex j-c flex1" style="margin: 100rpx 0;">
				<sb-img h='300' w='240' :src="$getStaticUrl('46163758c2ae4f54a75b6d9882325769.png')" />
			</view>
			<view class="flex flex-col j-end">
				<!-- <agreement-login v-model="agree" ref='argreeMent' class="mar20" /> -->
				<u-button openType="getPhoneNumber" :custom-style="{margin:'15rpx 100rpx'}"
					@getphonenumber='handleGetPhoneNumber' shape="circle" type="success" :loading="loading">
					微信登录
				</u-button>
				<u-button @click="go2PhoneLogin" :custom-style="{margin:'15rpx 100rpx'}" shape="circle" type="primary">
					手机号登录
				</u-button>
				<navigator open-type="switchTab" :url="HOME_PAGE_PATH" class="c-d t-c padv10">
					暂不登录
				</navigator>
			</view>
		</u-form>
	</view>

</template>

<script>
	import { KEY_SESSION_KEY, } from '@/config/constant'
	import { HOME_PAGE_PATH } from '@/config/index.js'
	import { login2wx } from '@/service/common'
	const app = getApp();
	export default {
		data() {
			return {
				agree: false,
				loading: false,
			}
		},
		onShow() {
			if (this.$checkLogin()) this.$go2(HOME_PAGE_PATH, true)
		},
		methods: {
			go2PhoneLogin() {
				// if (!this.agree) return this.$refs.argreeMent.showWarning()
				this.$go2('./phone-login')
			},
			async handleGetPhoneNumber({ detail: { encryptedData, iv ,code} }) {
				try {
console.log(code2);
					if (!encryptedData || !iv) return;
					// if (!this.agree) return this.$refs.argreeMent.showWarning()

					// this.loading = true

					let SESSION = this.$getCache('SESSION')
					if (!SESSION) SESSION = await login2wx()

					let { success, data } = await this.$postB("/wx/get-phone-no", {
						encryptedData,
						iv,
						SESSION
					},false)

					if (success) {
						this.$setToken(data)
						let { data: userInfo } = await this.$get('/user/info')
						this.$cacheMyInfo(userInfo)

						this.$msg('登录成功', async () => {
							if (data.newUser)
								this.$go2('./init-userinfo')
							else
								this.$redirectAfterLogin()
						})
					} else {
						this.$msg('登录失败，请稍后重试')
						this.removeCache(SESSION_KEY)
						this.$log({
							message,
							iv,
							sessionKey
						})
					}

					this.loading = false
				} catch (e) {
					this.$log('微信授权登录失败:' + e)
				}
			},
		}
	}
</script>

<style lang='scss' scoped>
	.form {
		height: 100vh;
	}
</style>
